/**
 * Created by Administrator on 15-9-28.
 */
window.onload=function() {
    var url = getUrl();
    var userid=getUserId();
    var urlList = getPics();
    $.jsonp({
        url: 'http://'+url+'/api/user/data',
        data: {userId:userid},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            var nickName=data.nickName;
            $("#nickName").val(nickName);
            var photo = data.photo;
            $("#photo").attr('src',photo);
            $(".ppt").attr('src',photo);
            var sex = data.sex;
            $("#sex").val(sex);
            var telphone=data.telphone;
            $("#telphone").val(telphone);
            var birthday=data.birthday;
            $("#birthday").val(birthday);
            var address=data.address;
            $("#address").val(address);
        },
        error: function (xOptions, textStatus) {

        }
    });
    $("#bc").on("click", function () {
        var nickName=$("#nickName").val();
        var sex=$("#sex").val();
        var telphone=$("#telphone").val();
        var birthday=$("#birthday").val();
        var address=  $("#address").val();
        //var id = $.getUrlParam("id");
        var urlList = $('#photo').attr('src');
        //alert("nickName==="+nickName);
        //alert("sex==="+sex);
        //alert("telphone==="+telphone);
        //alert("birthday==="+birthday);
        //alert("urlList==="+urlList);
        // alert("address==="+address);
        if(urlList == ""){
             showToast('请上传您的头像~');
             return;
        }

        if(nickName == ""){
            showToast('用户名不能为空~');
            return;
        }
        if(nickName.length>15){
            showToast('用户名长度不能大于15~');
            return;
        }
        if(sex == ""){
            showToast('性别不能为空~');
            return;
        }
        if(telphone == ""){
            showToast('手机号不能为空~');
            return;
        }
        if(telphone.length!=11){
            showToast('手机号格式不正确~');
            return;
        }
        if(birthday == ""){
            showToast('生日不能为空~');
            return;
        }

        $.jsonp({
            url: 'http://'+url+'/api/user/change_photo',
            data: {urlList:urlList,userId:userid},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                $.jsonp({
                     url: 'http://'+url+'/api/user/change_nickname',
                     data: {nickName:nickName,userId:userid},
                     callbackParameter: "callback",
                     success: function (data, textStatus, xOptions) {
                        $.jsonp({
                              url: 'http://'+url+'/api/user/change_sex',
                              data: {sex:sex,userId:userid},
                              callbackParameter: "callback",
                              success: function (data, textStatus, xOptions) {
                                    $.jsonp({
                                         url: 'http://'+url+'/api/user/change_telphone',
                                         data: {telphone:telphone,userId:userid},
                                         callbackParameter: "callback",
                                         success: function (data, textStatus, xOptions) {
                                              $.jsonp({
                                                    url: 'http://'+url+'/api/user/change_birthday',
                                                    data: {birthday:birthday,userId:userid},
                                                    callbackParameter: "callback",
                                                    success: function (data, textStatus, xOptions) {
                                                         window.location = "wode.html?birthday="+birthday;
                                                    },
                                                    error: function (xOptions, textStatus) {

                                                    }
                                               });
                                         },
                                         error: function (xOptions, textStatus) {

                                         }
                                     });
                              },
                              error: function (xOptions, textStatus) {

                              }
                        });
                     },
                     error: function (xOptions, textStatus) {

                     }
                });
            },
            error: function (xOptions, textStatus) {

            }
        });





//        $.jsonp({
//            url: 'http://'+url+'/api/user/add_address',
//            data: {address:address,userId:userid},
//            callbackParameter: "callback",
//            success: function (data, textStatus, xOptions) {
//                //if(data.succ==true){
//
//                    //取得用户ID
//                    //location.href="dizhi.html";
//                //}
//
//            },
//            error: function (xOptions,textStatus) {
//            }
//        });
        //console.log('href','wode.html?birthday='+birthday)


//        $("#bc").attr('href','wode.html?birthday='+birthday);
//        location.href='wode.html';
    });
    var currYear = (new Date()).getFullYear();
    var opt={};
    opt.date = {preset : 'date'};
    opt.datetime = {preset : 'datetime'};
    opt.time = {preset : 'time'};
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'modal', //显示方式
        mode: 'scroller', //日期选择模式
        dateFormat: 'yyyy-mm-dd',
        lang: 'zh',
        showNow: true,
        nowText: "今天",
        startYear: currYear - 50, //开始年份
        endYear: currYear + 10 //结束年份
    };

    $("#birthday").mobiscroll($.extend(opt['date'], opt['default']));
    var currYear = (new Date()).getFullYear();
    var opt={};
    opt.date = {preset : 'date'};
    opt.datetime = {preset : 'datetime'};
    opt.time = {preset : 'time'};
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'modal', //显示方式
        mode: 'scroller', //日期选择模式
        dateFormat: 'yyyy-mm-dd',
        lang: 'zh',
        showNow: true,
        nowText: "今天",
        startYear: currYear - 50, //开始年份
        endYear: currYear + 10 //结束年份
    };

    $("#endTime").mobiscroll($.extend(opt['date'], opt['default']));

}

function add_avatar(url){
    $("#photo").attr("src",url);
}